package com.sanjin.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sanjin.mall.product.entity.AttrAttrgroupRelationEntity;
import com.sanjin.mall.product.entity.AttrEntity;
import com.sanjin.mall.product.service.AttrAttrgroupRelationService;
import com.sanjin.mall.product.service.AttrService;
import com.sanjin.mall.product.service.CategoryService;
import com.sanjin.mall.product.vo.AttrAndGroupRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sanjin.mall.product.entity.AttrGroupEntity;
import com.sanjin.mall.product.service.AttrGroupService;
import com.sanjin.mall.common.utils.PageUtils;
import com.sanjin.mall.common.utils.R;



/**
 * 属性分组
 *
 * @author sanjin
 * @email 1032628545@qq.com
 * @date 2023-05-02 13:30:21
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){

        //1.查出当前分类下的所有属性分组
        //2.查出每个属性分组的所有属性
        List<AttrAndGroupRespVo> vos =   attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",vos);


    }

    // /product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationEntity> relEntity){

        relationService.saveBatch(relEntity);

        return R.ok();

    }

    // /product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){

        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);

        return R.ok().put("page",page);
    }


    // /product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody  List<AttrAttrgroupRelationEntity> list){
        //# todo peek使用后，必须加上最终操作才能生效
        list.stream().peek((x) -> relationService.remove(new QueryWrapper<AttrAttrgroupRelationEntity>()
            .eq("attr_id", x.getAttrId())
            .eq("attr_group_id", x.getAttrGroupId()))
        ).count();
        return R.ok();
    }

    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){

        List<AttrEntity> entities =  attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data",entities);
    }

    /**
     * 获取category对应的属性分组
     * 传入参数：key ：查询参数（groupname 和 groupid）
     */
    @RequestMapping("/list/{cateId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params
            ,@PathVariable("cateId") Integer cateId){
        PageUtils page = attrGroupService.queryPage(params, cateId);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catePath = categoryService.getCatelogPath(catelogId);
        attrGroup.setCatelogPath(catePath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
